####################################
# Descriptive
####################################

rm(list=ls())

library(Hmisc)
library(ggplot2)
library(stargazer)
library(foreign)
library(rdrobust)
library(rdd)
library(readstata13)

################
# Prepare data 
################

# read data
d = read.dta13("~/Dropbox/Anti-american attitudes/01_data/clean_data/trump_election_data_clean_final.dta")   
names(d)

# bandwidths
h_us_trust_bin = rdbwselect(d$us_trust_bin,d$score)$bws[1]
h_us_trust_bin

# prepare subset
d1 = d[abs(d$score)<=h_us_trust_bin,]
table(d1$score)

###########################
# Comparisons
###########################

us_trust_bin_d = mean(d$us_trust_bin)
female_d = mean(d$female)
age_d = mean(d$age, na.rm = T)
education_d = mean(d$education, na.rm = T)

us_trust_bin_d1 = mean(d1$us_trust_bin)
female_d1 = mean(d1$female)
age_d1 = mean(d1$age, na.rm = T)
education_d1 = mean(d1$education, na.rm = T)

mean_d = round(c(us_trust_bin_d,female_d,age_d,education_d),2)
mean_d1 = round(c(us_trust_bin_d1,female_d1,age_d1,education_d1),2)

colnames = c("US trust","Female","Age","Education")

table = data.frame(colnames,mean_d,mean_d1)
colnames(table) <- c("Variables","All units","Optimal Bandwidth")
table

stargazer(table, 
          type = "text",
          colnames = TRUE,
          summary = FALSE,
          title="", 
          digits=2, 
          rownames=FALSE, 
          float = TRUE, 
          float.env = "table", 
          table.placement = "H")
